<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card class="box-card">
      <!-- 搜索商品框 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <!-- 搜索区域 -->
          <el-input
            v-model="queryInfo.query"
            placeholder="请输入内容"
            class="input-with-select"
            clearable
            @clear="getGoodsList"
          >
            <!--
              clearable 清空数据  点击搜索时，请求数据获取对应的数据展示列表
              点击清空 删除搜索框中的数据，点击触发请求用户列表数据
            -->
            <!-- 点击搜索触发获取数据事件获取当前搜索内容 双向绑定的 -->
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="6">
          <el-button type="primary" plain @click="toAddGoods">添加商品</el-button>
        </el-col>
      </el-row>

      <!-- 商品列表表格 -->
      <el-table :data="goodsList">
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="680"></el-table-column>
        <el-table-column align="center" prop="goods_price" label="商品价格（元）"></el-table-column>
        <el-table-column align="center" prop="goods_weight" label="商品重量"></el-table-column>
        <el-table-column align="center" prop="add_time" label="创建时间"></el-table-column>

        <!-- 操作 -->
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" icon="el-icon-edit" type="warning" plain></el-button>
            <el-button
              size="mini"
              icon="el-icon-delete"
              type="danger"
              plain
              @click="removeGoods(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        query: '',
        // 当前页码
        pagenum: 1,
        // 每页显示的条数
        pagesize: 10
      },

      // 商品列表数据集合
      goodsList: [],

      // 所有的数据条数
      total: 0
    }
  },

  created() {
    // 调用获取商品列表属性方法
    this.getGoodsList()
  },

  methods: {
    // 编程式导航实现跳转
    toAddGoods() {
      this.$router.push('/goods/addgood')
    },

    // 点击删除按钮 删除商品
    async removeGoods(id) {
      const confirmResult = await this.$confirm('是否将该商品放入回收管理, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)

      // 点击取消按钮 表示取消了删除商品
      if (confirmResult !== 'confirm') return this.$message.info('您取消了删除')

      // 发送请求删除对应商品
      const { data: res } = await this.$http.delete(`/goods/${id}`)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.$message.success(res.meta.msg)
      // 重新加载数据
      this.getGoodsList()
    },

    // 监听当前每一页多少条
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    },

    // 监听当前页码值
    handleCurrentChange(newNum) {
      this.queryInfo.pagenum = newNum
      this.getGoodsList()
    },

    // 获取商品列表数据
    async getGoodsList() {
      const { data: res } = await this.$http.get('goods', { params: this.queryInfo })
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)

      res.data.goods.forEach(item => {
        item.add_time = this.$dayjs(item.add_time).format('YYYY-MM-DD hh:mm')
      })

      this.goodsList = res.data.goods
      this.total = res.data.total
    }
  }
}
</script>

<style lang="less" scoped>
</style>
